package com.boshao.classschedule.fragment;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.boshao.classschedule.R;
import com.boshao.classschedule.utils.Utils;
import com.boshao.classschedule.widget.TosGallery;
import com.boshao.classschedule.widget.WheelView;

/**
 * 选择课时Dialog
 * @author boshao
 *
 */
public class ClassTimeFragment extends DialogFragment implements View.OnClickListener{
	public static final String TAG = "time_fragment";
	View mRootView;
	
	WheelView mWheelWeek;
    WheelView mWheelOne;
    WheelView mWheelTwo;
    TextView mTvSave,mTvcancel;
    ArrayList<TextInfo> mWeeks = new ArrayList<TextInfo>();
    ArrayList<TextInfo> mOnes = new ArrayList<TextInfo>();
    ArrayList<TextInfo> mTwos = new ArrayList<TextInfo>();
    public static final int COUNT = 18;
    TextInfo mTIWeeks,mTIOnes,mTITwos;
    AddOrModifyClassFragment mAddOrModifyClassFragment;
	public static final String[] WEEKDAYS = {"周一","周二","周三","周四","周五","周六","周日"};
    
	
	public static ClassTimeFragment newInstance(
			AddOrModifyClassFragment addOrModifyClassFragment) {
		ClassTimeFragment fragment = new ClassTimeFragment();
		return fragment;
	}
	
	public void setAddOrModifyClassFragment(AddOrModifyClassFragment fragment){
		mAddOrModifyClassFragment = fragment;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(STYLE_NO_TITLE,0);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mRootView = inflater.inflate(R.layout.fragment_select_time,container,false);
		
		mWheelWeek = (WheelView) mRootView.findViewById(R.id.wheel_week);
		mWheelOne = (WheelView) mRootView.findViewById(R.id.wheel_one);
		mWheelTwo = (WheelView) mRootView.findViewById(R.id.wheel_two);

		mWheelWeek.setOnEndFlingListener(mListener);
		mWheelOne.setOnEndFlingListener(mListener);
		mWheelTwo.setOnEndFlingListener(mListener);

		mWheelWeek.setSoundEffectsEnabled(true);
		mWheelOne.setSoundEffectsEnabled(true);
		mWheelTwo.setSoundEffectsEnabled(true);

		mWheelWeek.setAdapter(new WheelTextAdapter(getActivity()));
		mWheelOne.setAdapter(new WheelTextAdapter(getActivity()));
		mWheelTwo.setAdapter(new WheelTextAdapter(getActivity()));
				
		initData();
		
		mTvcancel = (TextView) mRootView.findViewById(R.id.tv_cancel);
		mTvcancel.setOnClickListener(this);
		mTvSave = (TextView) mRootView.findViewById(R.id.tv_save);
		mTvSave.setOnClickListener(this);
		return mRootView;
	}
	
	private void initData() {
		for(int i = 0;i < COUNT;i++){
			mOnes.add(new TextInfo(i,String.format("第%d节",(i+1)),(i == 0)));
		}
		for(int i = 0;i < COUNT;i++){
			mTwos.add(new TextInfo(i,String.format("到%d节",(i+1)),(i == 0)));
			
		}
		for(int i = 0; i< WEEKDAYS.length;i++){
			mWeeks.add(new TextInfo(i,WEEKDAYS[i],(i == 0)));
		}
		
		((WheelTextAdapter) mWheelWeek.getAdapter()).setData(mWeeks);
        ((WheelTextAdapter) mWheelOne.getAdapter()).setData(mOnes);
        ((WheelTextAdapter) mWheelTwo.getAdapter()).setData(mTwos);
        
        mWheelWeek.setSelection(0);
        mTIWeeks = mWeeks.get(0);
        mWheelOne.setSelection(0);
        mTIOnes = mOnes.get(0);
        mWheelTwo.setSelection(0);
        mTITwos = mTwos.get(0);
	}
	
	private class TextInfo {
		public int mIndex;
		public String mText;
		public boolean mIsSelected = false;
		public int mColor = Color.BLACK;
        public TextInfo(int index, String text, boolean isSelected) {
            mIndex = index;
            mText = text;
            mIsSelected = isSelected;

//            if (isSelected) {
//                mColor = Color.BLUE;
//            }
        }
    }

	protected class WheelTextAdapter extends BaseAdapter {
        ArrayList<TextInfo> mData = null;
        int mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
        int mHeight = 50;
        Context mContext = null;

        public WheelTextAdapter(Context context) {
            mContext = context;
            mHeight = (int) Utils.pixelToDp(context, mHeight);
        }

        public void setData(ArrayList<TextInfo> data) {
            mData = data;
            this.notifyDataSetChanged();
        }

        public void setItemSize(int width, int height) {
            mWidth = width;
            mHeight = (int) Utils.pixelToDp(mContext, height);
        }

        @Override
        public int getCount() {
            return (null != mData) ? mData.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = null;

            if (null == convertView) {
                convertView = new TextView(mContext);
                convertView.setLayoutParams(new TosGallery.LayoutParams(mWidth, mHeight));
                textView = (TextView) convertView;
                textView.setGravity(Gravity.CENTER);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                textView.setTextColor(Color.BLACK);
            }
            if (null == textView) {
                textView = (TextView) convertView;
            }
            TextInfo info = mData.get(position);
            textView.setText(info.mText);
            textView.setTextColor(info.mColor);

            return convertView;
        }
    }
	
	
	private TosGallery.OnEndFlingListener mListener = new TosGallery.OnEndFlingListener() {
        @Override
        public void onEndFling(TosGallery v) {
            int pos = v.getSelectedItemPosition();
            if(v == mWheelWeek){
            	mTIWeeks = mWeeks.get(pos);
            }else if(v == mWheelOne){
            	mTIOnes = mOnes.get(pos);
            }else{
            	mTITwos = mTwos.get(pos);
            }
        }
    };

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_cancel:
			dismiss();
			break;
		case R.id.tv_save:
			save();
			break;
		}
	}

	private void save() {
		if(mTITwos.mIndex > mTIOnes.mIndex){
			System.out.println(mTIWeeks.mText + " "+mTIOnes.mText+" "+mTITwos.mText);	
			mAddOrModifyClassFragment.mTvTime.setText(String.format("%s %d-%d节",mTIWeeks.mText,
					Integer.parseInt(mTIOnes.mText.substring(1,2)),
					Integer.parseInt(mTITwos.mText.substring(1,2)))
					);
			dismiss();
		}else{
			Toast.makeText(getActivity(),"不好意思！选择第二个节数要比第一个大",Toast.LENGTH_LONG).show();
		}
	}

	
}
